package _16_lianxi;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Course {
	static Map<Integer, List<Student>> students = new HashMap<Integer, List<Student>>();

	public static void main(String[] args) {
		Student s1 = new Student(1, 11, "张三1", 1);
		Student s2 = new Student(1, 11, "张三1", 2);
		Student s3 = new Student(2, 11, "张三3", 1);
		Student s4 = new Student(2, 11, "张三3", 2);
		Student s5 = new Student(3, 11, "张三5", 1);
		Student s6 = new Student(4, 11, "张三6", 1);
		Student s7 = new Student(5, 11, "张三7", 1);
		Student s8 = new Student(5, 11, "张三8", 2);
		Student s9 = new Student(7, 11, "张三9", 1);
		Student s10 = new Student(8, 11, "张三10", 1);
		add(s1);
		add(s2);
		add(s3);
		add(s4);
		add(s5);
		add(s6);
		add(s7);
		add(s8);
		add(s9);
		add(s10);

		// 获取所有的value ,判断元素个数,如果为 2 的,就说明是选修两门课程的
		Collection<List<Student>> values = students.values();

		List<Integer> nos = new ArrayList<Integer>();
		int count = 0;
		for (List<Student> list : values) {
			if (list.size() == 2) {
				count++;
				nos.add(list.get(0).no);
			}
		}
		System.out.println(count);
		System.out.println(nos);

		for (Entry<Integer, List<Student>> entry : students.entrySet()) {
			System.out.println(entry);
		}
	}

	public static void add(Student student) {
		// 判断是否包含某个学生
		if (students.containsKey(student.no)) {
			// 如果包含,说明是第二次添加,就根据学号获取对应的ArrayList 并把课程添加到 ArrayList 中
			List<Student> list = students.get(student.no);
			list.add(student);
			return;
		}
		// 如果不包含,说明是第一次添加,创建 ArrayList 并保存课程
		List<Student> list = new ArrayList<Student>();
		list.add(student);
		students.put(student.no, list);
	}
}

class Student {
	int no;
	int age;
	String name;
	int c;// 所选课程

	public Student(int no, int age, String name, int c) {
		super();
		this.no = no;
		this.age = age;
		this.name = name;
		this.c = c;
	}

	@Override
	public String toString() {
		return "Student [no=" + no + ", age=" + age + ", name=" + name + ", c=" + c + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Student) {
			Student s = (Student) obj;
			if (s.no == this.no) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return no;
	}
}